package com.zzxx.photograph.dao.impl;

import com.zzxx.photograph.dao.UserDao;
import com.zzxx.photograph.domain.User;
import com.zzxx.photograph.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDaoImpl implements UserDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public int saveUser(User user) {
        String sql = "insert into user" +
                "(username, password, name, birthday, sex, telephone, email, status, code) " +
                "values(?,?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(),
                user.getName(), user.getBirthday(), user.getSex(),
                user.getTelephone(), user.getEmail(), user.getStatus(), user.getCode());
    }

    @Override
    public User findUserByUsername(String username) {
        String sql = "select * from user where username = ?";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), username);
        return list.size() == 0 ? null : list.get(0);
    }

    @Override
    public int updateUserStatusByCode(String code) {
        String sql = "update user set status = 'Y' where code = ?";
        return jdbcTemplate.update(sql, code);
    }

    @Override
    public User findByUsernameAndPwd(String username, String password) {
        String sql = "select * from user where username = ? and password = ?";
//        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
//        return list.size() == 0 ? null : list.get(0);
        try {
            User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
            return user;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void updatePasswordByUsername(String username, String password) {
        String sql = "update user set password=? where username=?";
        jdbcTemplate.update(sql, password, username);
    }

    @Override
    public User getUserByUid(int uid) {
        String sql = "select * from user where uid = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), uid);
    }

    @Override
    public void update(String uid, User user) {
        String sql = "update user set username = ? , name = ? , telephone = ? ,email = ? where uid = ?";
        jdbcTemplate.update(sql, user.getUsername(), user.getName(), user.getTelephone(), user.getEmail(), uid);
    }

    @Override
    public void deleteUserByUid(String uid) {
        String sql = "delete from user where uid = ?";
        jdbcTemplate.update(sql, uid);
    }

    @Override
    public String findUsernameByEmail(String email) {
        String sql = "select username from user where email=?";
        return jdbcTemplate.queryForObject(sql, String.class, email);
    }

}
